package com.matrix.framework.sysconfig.token.repositories;

import com.matrix.framework.sysconfig.token.data.SysConfigPo;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 系统配置仓库
 * 
 * Copyright © 海平面工作室 版权所有
 *
 * @Author: Leo
 * @Create: 2025/06/23 23:25
 * @Since 1.2
 */
@Repository
public interface SysConfigRepository extends ReactiveCrudRepository<SysConfigPo, String> {

    /**
     * 根据配置分类查询配置项
     */
    Flux<SysConfigPo> findBySort(String sort);

    /**
     * 根据配置键查询配置项
     */
    Mono<SysConfigPo> findByKey(String key);

    /**
     * 根据配置分类和键查询配置项
     */
    Mono<SysConfigPo> findBySortAndKey(String sort, String key);

    /**
     * 检查配置键是否存在
     */
    @Query("SELECT COUNT(*) FROM sys_config WHERE `key` = :key")
    Mono<Long> countByKey(String key);

    /**
     * 删除配置项
     */
    @Modifying
    @Query("DELETE FROM sys_config WHERE `key` = :key")
    Mono<Void> deleteByKey(String key);

    /**
     * 根据配置分类和键删除配置项
     */
    @Modifying
    @Query("DELETE FROM sys_config WHERE sort = :sort AND `key` = :key")
    Mono<Void> deleteBySortAndKey(String sort, String key);

    /**
     * 更新配置值
     */
    @Modifying
    @Query("UPDATE sys_config SET value = :value, memo = :memo WHERE `key` = :key")
    Mono<Void> updateByKey(String key, String value, String memo);

    /**
     * 根据配置分类和键更新配置值
     */
    @Modifying
    @Query("UPDATE sys_config SET value = :value, memo = :memo WHERE sort = :sort AND `key` = :key")
    Mono<Void> updateBySortAndKey(String sort, String key, String value, String memo);
} 